package com.af.system.util;

import java.lang.reflect.Array;
import java.nio.charset.Charset;
import java.time.ZoneId;
import java.util.*;

/**
 * @author : zhenyun.su
 * @comment : object 工具类
 * @since : 2022/5/19
 */

public abstract class AfObjectUtils {
    public static Charset UTF_8 = Charset.forName("UTF-8");
    public static final ZoneId zoneId = ZoneId.of("Asia/Shanghai");
    // public static final TimeZone timeZone = TimeZone.getTimeZone("GMT+8:00");
    public static final TimeZone timeZone = TimeZone.getTimeZone(zoneId);
    public static final Locale locale = Locale.CHINA;

    public static boolean isEmpty(Object[] array) {
        return array == null || array.length == 0;
    }

    public static boolean isEmpty(Object obj) {
        if (obj == null) {
            return true;
        } else if (obj instanceof Optional) {
            return !((Optional)obj).isPresent();
        } else if (obj instanceof CharSequence) {
            return ((CharSequence)obj).length() == 0;
        } else if (obj.getClass().isArray()) {
            return Array.getLength(obj) == 0;
        } else if (obj instanceof Collection) {
            return ((Collection)obj).isEmpty();
        } else {
            return obj instanceof Map ? ((Map)obj).isEmpty() : false;
        }
    }

    public static void emptyError(Object obj, String error) throws Exception {
        if (isEmpty(obj)){
            throw new Exception(error);
        }
    }

    public static void emptyError(Object[] array, String error) throws Exception {
        if (isEmpty(array)){
            throw new Exception(error);
        }
    }

    public static void emptyErrorMsg(Object obj, String msg) throws Exception {
        if (isEmpty(obj)){
            throw new Exception(msg);
        }
    }

}
